import { preOrderApi, payPayment, freedomPay, getEpay } from "@/api/order.js";
import util from "@/utils/util";
import { halyk } from "@/static/js/epay.js";
/**
 * 去商品详情
 */
export function goShopDetail(item) {
	uni.navigateTo({
		url: `/pages/goods_details/index?id=${item.id}`,
	});
}

/**
 * 活动商品、普通商品、购物车、再次购买预下单
 */
export function getPreOrder(preOrderType, orderDetails) {
	return new Promise((resolve, reject) => {
		preOrderApi({
			preOrderType: preOrderType,
			orderDetails: orderDetails,
		})
			.then((res) => {
				uni.navigateTo({
					url:
						"/pages/users/order_confirm/index?preOrderNo=" +
						res.data.preOrderNo,
				});
			})
			.catch((err) => {
				return util.Tips({
					title: err,
				});
			});
	});
}

/**
 * 待支付订单去付款
 */
export function goPay(item) {
	console.log("item", item);
	return new Promise((resolve, reject) => {
		if (item.payType === "offline") {
			uni.navigateTo({
				url:
					"/pages/order_details/index?order_id=" +
					item.orderNo +
					"&orderStatus=" +
					0,
			});
			return;
		}
		if (item.payType === "epay-zk") {
			getEpay({
				mallOrderNo: item.orderNo,
				payChannel: "mobile",
			}).then((res) => {
				let domain = location.hostname;
				let port = location.port;
				let jumpUrl = `${domain}:${port}/pages/users/order_list/index?status=1`;
				res.data.auth = res.data.access_token;
				res.data.failureBackLink = jumpUrl;
				res.data.failureBackLink = jumpUrl;
				res.data.backLink = jumpUrl;
				halyk.pay(res.data);
			});
			return;
		}

		if (item.payType === "freedom") {
			let payChannel = "h5";
			// #ifdef APP
			payChannel = "app";
			// #endif
			freedomPay({
				mallOrderNo: item.orderNo,
				payChannel: payChannel,
			}).then((res) => {
				// uni.navigateTo({url:`/pages/webview/index?url=${res.data}`})
				// #ifdef APP
				// plus.runtime.openWeb(res.data);
				uni.navigateTo({ url: `/pages/webview/index?url=${res.data}` });
				// #endif
				// #ifdef H5
				window.location.href = res.data;
				// #endif
			});
			return;
		}
		payPayment({
			orderNo: item.orderNo,
			payChannel: item.payChannel,
			payType: item.payType,
		})
			.then((res) => {
				if (item.payType === "paypal") {
					window.location.href = res.data.redirect;
				} else {
					uni.navigateTo({
						url:
							"/pages/users/stripe_payment/index?clientSecret=" +
							res.data.stripeClientSecret,
					});
				}
				uni.hideLoading();
			})
			.catch((err) => {
				uni.hideLoading();
				return util.Tips({
					title: err,
				});
			});
	});
}
